#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Mikan リクエスト パッケージ

Mikan Python Web Framework
"""

__author__ = "K.Sonohara"
__status__ = "develop"
__version__ = "0.0.0_33"
__date__    = "2013/12/08"
__license__ = 'LGPL'


# ------------------------------------------------------------
## インポート
# Python
import traceback
from xml.dom import minidom

# XML
try:
	import libxml2
	import libxslt
except:
	pass

# ESiPC
from esipc import ESiPCProperty

# Mikan
from mikan.viewlet import MikanViewlet

# ------------------------------------------------------------
## Mikan メニューバー ビューレットクラス
#
class MikanSXBViewlet(MikanViewlet):

	def set_sxb(self, value):
		self.FSXB = value

	def get_sxb(self):
		return self.FSXB

	def set_htdocs(self, value):
		self.FHtdocs = value

	def get_htdocs(self):
		return self.FHtdocs

	def set_indexes(self, value):
		self.FIndexes = value

	def is_indexes(self):
		return self.FIndexes

	def set_tree_indexes(self, value):
		self.FTreeIndexes = value

	def is_tree_indexes(self):
		return self.FTreeIndexes

	def set_template(self, value):
		self.FTemplate = value

	def get_template(self):
		return self.FTemplate

	def set_content(self, value):
		self.FContent = value

	def is_content(self):
		return self.FContent

	def get_params(self):
		return self.FParams

	def __init__(self, parent, sxb, htdocs=None, indexes=False, content=True, template="bootstrap"):
		super(MikanSXBViewlet, self).__init__(parent)

		self.FTemplate = template
		self.FSXB = sxb
		self.FHtdocs = htdocs
		self.FIndexes = indexes
		self.FContent = content
		self.FTreeIndexes = False
		self.FParams = { "text.top": "'↑'" }

	def perform(self):
		if super(MikanSXBViewlet, self).perform() == False:
			return False

		if self.iscontent and self.isindexes:
			xslt_name = self.request.mikanroot +  '/sxmlbook/xslt/' + self.template + '/mikan.xsl'
		elif self.isindexes:
			xslt_name = self.request.mikanroot +  '/sxmlbook/xslt/' + self.template + '/mikan-indexes.xsl'
		else:
			xslt_name = self.request.mikanroot +  '/sxmlbook/xslt/' + self.template + '/mikan-item.xsl'
		if self.islogdebug:
			self.logger.debug(xslt_name)
		styleDoc = libxml2.parseFile(xslt_name)
		xslt = libxslt.parseStylesheetDoc(styleDoc)

		params = self.params
		if self.htdocs:
			params["html.dir"] = "'%s'"  % self.htdocs
		if not self.istreeindexes:
			params["tree.indexes"] = "'0'"
		params["clazz.title"] = "'var'"

		if self.islogdebug:
			self.logger.debug(self.htdocs)
			self.logger.debug(params)

		xml = libxml2.parseFile(self.sxb)
		html = xslt.applyStylesheet(xml, params)

		# log
		if self.islogdebug:
			self.logger.debug(xml)
			self.logger.debug(html)

		s = html.__str__()
		try:
			s = s.decode("utf_8").encode("utf-8")
			s = s.replace(" xmlns=\"http://www.w3.org/1999/xhtml\"", "")
			s = s.replace(" xmlns:xhtml=\"http://www.w3.org/1999/xhtml\"", "")
		except:
			self.logger.error(traceback.format_exc())

		html = minidom.parseString(s)

		self.element = html.documentElement.cloneNode(True)
		self.parent.element.appendChild(self.element)

		return True

	# ----------------------------------------------------
	## Property
	template = ESiPCProperty(get_template, set_template, None)
	isindexes = ESiPCProperty(is_indexes, set_indexes, None)
	istreeindexes = ESiPCProperty(is_tree_indexes, set_tree_indexes, None)
	iscontent = ESiPCProperty(is_content, set_content, None)
	sxb = ESiPCProperty(get_sxb, set_sxb, None)
	htdocs = ESiPCProperty(get_htdocs, set_htdocs, None)
	params = ESiPCProperty(get_params, None, None)
